package bancodedados;

import java.sql.ResultSet;
import java.sql.SQLException;

import userInterface.Atendente;
import userInterface.RH;
import userInterface.Usuario;



import exceptions.UsuarioInexistente;

public class LoginResultSetDataFetcher extends ResultSetDataFetcher<Usuario> {
	private static final String TABLENAME		= "login";
	private static final String ID				= "id";
	private static final String LOGIN			= "login";
	private static final String PASSWORD		= "password";
	private static final String IS_RH			= "is_rh";
	private static final String IS_ATENDENTE	= "is_atendente";
	
	private String login;
	private String password;
	
	public LoginResultSetDataFetcher(String login, String password){
		this.login = login;
		this.password = password;
	}
	
	@Override
	public void fetchDataFromResultSet(ResultSet resultSet) throws SQLException, UsuarioInexistente {
		if (resultSet.next()) {
			if (resultSet.getBoolean(IS_RH)){
				results.add(new RH(resultSet.getInt(ID), 
										resultSet.getString(LOGIN)
									)
						);
			}
			else if(resultSet.getBoolean(IS_ATENDENTE)){
				results.add(new Atendente(resultSet.getInt(ID), 
										resultSet.getString(LOGIN)
									)
						);
			}
			else {
				results.add(new Usuario(resultSet.getInt(ID), 
										resultSet.getString(LOGIN)
									)
						);
			}
		} else {
			throw new UsuarioInexistente(login);
		}
	}
	
	@Override
	public String getSelectionString() {
		return 	SELECT + " \"" + ID + "\", \"" + LOGIN + "\", \"" + IS_RH + "\", \"" + IS_ATENDENTE + "\" " +
				FROM + " \"" + TABLENAME + "\" " +
				WHERE + " \"" + LOGIN    + "\" = '" + this.login    + "' AND " +
						" \"" + PASSWORD + "\" = '" + this.password + "'";
	}
	
}
